﻿using IntoCommon.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace IntoStationRepair.PLC
{
    public class MsgHelper
    {
        public static string MsgDir = Path.Combine(@"D:\", "datas", "msg");
        public static string StepTipConfigFile => Path.Combine(MsgDir, "StepTipConfig.xml");
        public static string TipInfoConfigFile => Path.Combine(MsgDir, "TipInfoConfig.xml");
        public static string AlarmConfigFile => Path.Combine(MsgDir, "AlarmConfig.xml");
        public static string WarningConfigFile => Path.Combine(MsgDir, "WarningConfig.xml");
        public static Dictionary<int, string> AlarmDic = new Dictionary<int, string>();

        public static List<StationInfo> StepTipDic = new List<StationInfo>();

        public static Dictionary<int, string> TipInfoDic = new Dictionary<int, string>();

        public static Dictionary<int, string> WarningDic = new Dictionary<int, string>();

        private static void LoadDic(string file, Dictionary<int, string> dic)
        {
            if (!Directory.Exists(MsgDir))
            {
                Directory.CreateDirectory(MsgDir);
            }
            if (File.Exists(file))
            {
                var datas = GlobalFunc.LoadXmlFile<List<Pair>>(file);
                foreach (var data in datas)
                {
                    dic.Add(data.Key, data.Desc);
                }
            }
            else
            {
                List<Pair> datas = new List<Pair>();
                for (int i = 0; i < 32; i++)
                {
                    Pair pair = new Pair() { Key = i + 1, Desc = $"test{i + 1}" };
                    datas.Add(pair);
                }
                GlobalFunc.WriteXmlFile(file, datas);
            }
        }
        private static void LoadStep(string file,ref List<StationInfo> datas)
        {
            if (!Directory.Exists(MsgDir))
            {
                Directory.CreateDirectory(MsgDir);
            }
            if (File.Exists(file))
            {
                datas = GlobalFunc.LoadXmlFile<List<StationInfo>>(file);
            }
            else
            {
                List<StationInfo> infos = new List<StationInfo>();
                for (int i = 0; i < 4; i++)
                {
                    StationInfo info = new StationInfo();
                    info.Name = $"OP{i + 1}0";
                    for (int j = 0; j < 10; j++)
                    {
                        Pair pair = new Pair() { Key = j + 1, Desc = $"{info.Name} test{j + 1}" };
                        info.Infos.Add(pair);
                    }
                    infos.Add(info);
                }
                GlobalFunc.WriteXmlFile(file, infos);
            }
        }

        public static void Init()
        {
            LoadDic(AlarmConfigFile, AlarmDic);
            LoadStep(StepTipConfigFile,ref StepTipDic);
            LoadDic(TipInfoConfigFile, TipInfoDic);
            LoadDic(WarningConfigFile, WarningDic);
        }
    }


    [Serializable]
    public class Pair
    {
        [XmlAttribute]
        public int Key { get; set; }

        [XmlAttribute]
        public string Desc { get; set; }
    }

    [Serializable]
    public class StationInfo
    {
        [XmlAttribute]
        public string Name { get; set; }
        [XmlElement]
        public List<Pair> Infos { get; set; } = new List<Pair>();
    }
}
